"use server";
import { BannerRep, GroupType, NoticeRep, PromotionRep } from "@/api/home";
import Box from "@/components/Box";
import HomeActions from "./_home/HomeActions";
import HomeCard from "./_home/HomeCard";
import HomeGames from "./_home/HomeGames";
import HomeMessage from "./_home/HomeMessage";
import HomeNoticeBar from "./_home/HomeNoticeBar";
import HomePrize from "./_home/HomePrize";
import HomePromotion from "./_home/HomePromotion";
import HomeSearch from "./_home/HomeSearch";
import HomeSwiper from "./_home/HomeSwiper";
import HomeTabs from "./_home/HomeTabs";
const BASE_URL = process.env.NEXT_PUBLIC_BASE_URL;

const TIME = 30;
const getGames = async (): Promise<GroupType[]> => {
    try {
        const data = await fetch(`${BASE_URL}/v1/api/front/game_list`, {
            method: "POST",
            body: JSON.stringify({ template_key: "g_temp_9" }),
            next: { revalidate: TIME },
        }).then((res) => res.json());
        console.log(`🎯🎯🎯🎯🎯-> in page.tsx on 24`, data.data);
        return data.data;
    } catch (err) {
        return [];
    }
};
const getBanners = async (): Promise<BannerRep[]> => {
    try {
        const response = await fetch(`${BASE_URL}/v1/api/front/banner_list`, {
            method: "POST",
            body: JSON.stringify({}),
            headers: {
                language: "zh",
            },
            next: { revalidate: TIME },
        }).then((res) => res.json());
        return response.data;
    } catch (err) {
        return Promise.resolve([]);
    }
};

const getNotices = async (): Promise<NoticeRep[]> => {
    try {
        const response = await fetch(`${BASE_URL}/v1/api/front/marquee`, {
            method: "POST",
            body: JSON.stringify({}),
            headers: {
                language: "zh",
            },
            next: { revalidate: TIME },
        }).then((res) => res.json());
        return response.data;
    } catch (err) {
        return [];
    }
};
const getActivities = async (): Promise<BannerRep[]> => {
    try {
        const response = await fetch(`${BASE_URL}/v1/api/front/activity_home`, {
            method: "POST",
            body: JSON.stringify({}),
            headers: {
                language: "zh",
            },
            next: { revalidate: TIME },
        }).then((res) => res.json());
        return response.data;
    } catch (err) {
        return [];
    }
};

const getPromotions = async (): Promise<PromotionRep[]> => {
    try {
        const response = await fetch(`${BASE_URL}/v1/api/front/pop_list`, {
            method: "POST",
            body: JSON.stringify({}),
            headers: {
                language: "zh",
            },
            next: { revalidate: TIME },
        }).then((res) => res.json());
        return response.data;
    } catch (err) {
        return [];
    }
};

export default async function Page() {
    const [group = [], banners = [], notices = [], activities = [], promotions = []] =
        await Promise.all([
            getGames(),
            getBanners(),
            getNotices(),
            getActivities(),
            getPromotions(),
        ]);
    return (
        <div>
            <HomeMessage />
            <HomePromotion data={promotions} />
            <Box>
                <HomeSwiper banners={banners}></HomeSwiper>
                <HomeCard banners={activities}></HomeCard>
                <HomeNoticeBar notices={notices} />
                <HomeSearch />
                <HomePrize></HomePrize>
                <HomeTabs />
            </Box>
            {group.map((item) => {
                return <HomeGames groupGames={item.category} key={item.category_name} />;
            })}
            <Box>
                <HomeActions />
            </Box>
        </div>
    );
}
